<template>
  <div class="xtx-form">
    <div class="user-info">
      <img
        :src="imgHeader"
        alt=""
      />
      <p>Hi，{{nickname}}欢迎来小兔鲜，完成绑定后可以QQ账号一键登录哦~</p>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-phone"></i>
        <input class="input" type="text" placeholder="绑定的手机号" v-model="tel" />
      </div>
      <div class="error"></div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-code"></i>
        <input class="input" type="text" placeholder="短信验证码" />
        <span class="code" @click="send" v-if="!show">发送验证码</span>
        <div class="code" v-if="show">{{ times }}后发送</div>
      </div>
      <div class="error"></div>
    </div>
    <a href="javascript:;" class="submit" @click="bang()" >立即绑定</a>
  </div>
</template>

<script>
import { sendApi, bindApi } from "@/Api/loginApi"
export default {
  name: "CallbackBind",
  props: ["imgHeader", "nickname", "openid"],
  data () {
    return {
      tel: "",
      show: false,
      times: 5
    };
  },
  methods: {
    // 发送验证码
    send () {
      sendApi(this.tel, this.openid).then((res) => {
        // console.log(res);
        this.$message({ text: res.data.msg, type: "success" })
      }).catch((err) => {
        // console.log(err);
        // 验证码已发送
        if (err.code == "17008") {
          this.show = true
          var te = setInterval(() => {
            if (this.times <= 1) {
              clearInterval(te)
              this.show = false
            }
            this.times--
          }, 1000)
          this.$message({ text: err.message, type: "success" })
        }
        // 用户名不存在      
        if (err.code == "17001") {
           this.$emit("sui")
        }
        this.$message({ text: err.message, type: "error" })
      })
    },
    // 有账号没绑定的情况 绑定信息
    bang () {
      bindApi(this.openid, this.tel, '123456').then((res) => {
        console.log(res);
        this.$message({ text: "登录成功", type: "success" })
        let { id, account, avatar, nickname, token } = res.data.result
        this.$store.commit("Homestatic/setuser", { id, account, avatar, nickname, token })
        this.$router.push("/")
      }).catch((err) => {
        // console.log(err);
        this.$message({ text: err.message, type: "error" })
      })
    }
  },
};
</script>

<style scoped lang="scss">
@import "@/assets/css/variables.scss";
.user-info {
  width: 320px;
  height: 70px;
  margin: 0 auto;
  display: flex;
  background: #f2f2f2;
  align-items: center;
  padding: 0 10px;
  margin-bottom: 25px;
  img {
    background: #f2f2f2;
    width: 50px;
    height: 50px;
  }
  p {
    padding-left: 10px;
  }
}
.code {
  position: absolute;
  right: 0;
  top: 0;
  line-height: 50px;
  width: 80px;
  color: #999;
  &:hover {
    cursor: pointer;
  }
}

// 表单
.xtx-form {
  padding: 50px 0;
  &-item {
    display: flex;
    align-items: center;
    width: 700px;
    margin: 0 auto;
    padding-bottom: 25px;
    .label {
      width: 180px;
      padding-right: 10px;
      text-align: right;
      color: #999;
      ~ .field {
        margin-left: 0;
      }
    }
    .field {
      width: 320px;
      height: 50px;
      position: relative;
      margin-left: 190px;
      .icon {
        position: absolute;
        left: 0;
        top: 0;
        width: 40px;
        height: 50px;
        text-align: center;
        line-height: 50px;
        color: #999;
        ~ .input {
          padding-left: 40px;
        }
      }
      .input {
        border: 1px solid #e4e4e4;
        width: 320px;
        height: 50px;
        line-height: 50px;
        padding: 0 10px;
        &.err {
          border-color: $priceColor;
        }
        &:focus,
        &:active {
          border-color: $xtxColor;
        }
      }
    }
    .error {
      width: 180px;
      padding-left: 10px;
      color: $priceColor;
    }
  }
  .submit {
    width: 320px;
    height: 50px;
    border-radius: 4px;
    background: $xtxColor;
    height: 50px;
    line-height: 50px;
    text-align: center;
    font-size: 16px;
    color: #fff;
    display: block;
    margin: 0 auto;
  }
}
</style>
